use crate::entities::{
    prelude::ZaUser,
    za_user::{self, ActiveModel},
};
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, QuerySelect};

pub struct UserModel<'a> {
    conn: &'a sea_orm::DatabaseConnection,
}

#[allow(dead_code)]
impl<'a> UserModel<'a> {
    pub fn new(conn: &'a sea_orm::DatabaseConnection) -> Self {
        UserModel { conn: conn }
    }
    pub async fn get_by_id(self, id: i64) -> Result<Option<za_user::Model>, sea_orm::DbErr> {
        ZaUser::find()
            .filter(za_user::Column::Id.eq(id))
            .one(self.conn)
            .await
    }
    pub async fn get_by_user_id(
        self,
        user_id: String,
    ) -> Result<Option<za_user::Model>, sea_orm::DbErr> {
        ZaUser::find()
            .filter(za_user::Column::UserId.eq(user_id))
            .one(self.conn)
            .await
    }
    pub async fn create(self, active_model: ActiveModel) -> Result<za_user::Model, sea_orm::DbErr> {
        active_model.insert(self.conn).await
    }
    pub async fn fetch(
        self,
        offset: u32,
        limit: u32
    ) -> Result<Vec<za_user::Model>, sea_orm::DbErr> {
        ZaUser::find()
            .offset(offset as u64)
            .limit(limit as u64)
            .all(self.conn)
            .await
    }
}
